package model
import(
	"fmt"
	"github.com/garyburd/redigo/redis"
	"encoding/json"
	"manyuser/common/message"
)
var (
	MyUserDao *UserDao
)
type UserDao struct{
	pool *redis.Pool
}

//工厂模式
func NewUserDao(pool *redis.Pool)(userDao *UserDao){
	userDao=&UserDao{pool,}
	return
}
//返回User示例
func (this *UserDao)getUserById(conn redis.Conn ,id int)(user *User,err error){
	res,err:=redis.String(conn.Do("HGET","users",id))
	if err!=nil{
		if err==redis.ErrNil{
			err=ERROR_USER_NOTEXISTS
		}
		return
	}
	user=&User{}
	err=json.Unmarshal([]byte(res),user)
	if err!=nil{
		fmt.Println("json.Unmarshal([]byte(res),user) wrong:",err)
		return
	}
	return
}

//登录校验
func(this *UserDao)Login(userId int,userPwd string)(user *User,err error){
	conn:=this.pool.Get()
	defer conn.Close()
	user,err=this.getUserById(conn,userId)
	if err!=nil{
		return
	}
	if userPwd!=user.UserPwd{
		err=ERROR_USER_PWD  
		return
	}
	return
}

//注册用户
func(this *UserDao)Register(user *message.User)(err error){
	conn:=this.pool.Get()
	defer conn.Close()

	_,err=this.getUserById(conn,user.UserId)
	if err==nil{
		err=ERROR_USER_EXISTS
		return
	}
	data,err:=json.Marshal(user)
	if err!=nil{
		return
	}
	_,err=conn.Do("Hset","users",user.UserId,string(data))
	if err!=nil{
		fmt.Println("register is wrong",err)
		return
	}
	return
}